﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq.Expressions;
using System.Text;
using System.Windows.Forms;

namespace NapazLevelMaker
{
    public partial class frmLevelMaker : Form
    {
        private Panel controlsPanel;
        private Panel imagesPanel;
        private Panel eventPanel;
        private Texture selectedTexture;
        private bool dragging;


        public frmLevelMaker()
        {
            InitializeComponent();

            controlsPanel = splitContainer.Panel1;
            controlsPanel.Name = "ControlsPanel";

            imagesPanel = splitContainer.Panel2;
            imagesPanel.Name = "ImagesPanel";

            eventPanel = pnlEventPanel;
            eventPanel.Name = "EventPanel";

            splitContainer.Resize += new EventHandler(SplitContainerResized);
            
            eventPanel.Size = imagesPanel.Size;
            splitContainer.SendToBack();

            RegisterEvents(eventPanel);
        }


        /// <summary>
        /// Handles the window resize event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SplitContainerResized(object sender, EventArgs e)
        {
            eventPanel.Size = imagesPanel.Size;
        }

        /// <summary>
        /// Registers the events for the given control
        /// </summary>
        /// <param name="c"></param>
        private void RegisterEvents(Control c)
        {
            c.MouseDown += new MouseEventHandler(MouseDownEvent);
            c.MouseMove += new MouseEventHandler(MouseMoveEvent);
            c.MouseUp += new MouseEventHandler(MouseUpEvent);
        }

        /// <summary>
        /// When the Add texture button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "All Files (*.*)|*.*|png files (*.png)|*.png|jpg files (*.jpg)|*.jpg";
            ofd.Multiselect = true;

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    for (int i = 0; i < ofd.SafeFileNames.Length; i++)
                    {
                        Texture t = new Texture();
                        t.Name = ofd.SafeFileNames[i];
                        t.Path = ofd.FileNames[i];
                        t.Position = new Point(0, 300);
                        t.Image = Image.FromFile(t.Path);
                        t.ZIndex = 0;

                        if (!lstTextures.Items.Contains(t))
                        {
                            lstTextures.Items.Add(t);

                            PictureBox pb = new PictureBox();
                            pb.Image = t.Image;
                            pb.Location = t.Position;
                            pb.Name = t.Id.ToString();

                            RegisterEvents(pb);

                            imagesPanel.Controls.Add(pb);
                            imagesPanel.Controls.SetChildIndex(pb, t.ZIndex);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }

        /// <summary>
        /// When the Delete texture is pressed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDeleteTexture_Click(object sender, EventArgs e)
        {
            for (int i = lstTextures.Items.Count; i > -1; i--)
            {
                if (lstTextures.SelectedIndices.Contains(i))
                {
                    //remove from list box
                    Texture t = (Texture)(lstTextures.Items[i]);
                    lstTextures.Items.RemoveAt(i);
                    
                    //remove from images panel
                    imagesPanel.Controls.RemoveByKey(t.Id.ToString());
                }
            }
        }

        /// <summary>
        /// Handles the Panel2MouseDown event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MouseDownEvent(object sender, EventArgs e)
        {
            if (lstTextures.SelectedIndices.Count > 0)
            {
                selectedTexture = (Texture)(lstTextures.Items[lstTextures.SelectedIndex]);
                SetTextureLocation(selectedTexture);
                dragging = true;
            }
            else
            {
                selectedTexture = null;
            }
        }

        /// <summary>
        /// Handles the Panel2MouseMove event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MouseMoveEvent(object sender, EventArgs e)
        {
            if (dragging)
            {
                SetTextureLocation(selectedTexture);
            }
        }

        /// <summary>
        /// Handles the Panel2MouseUpEvent
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MouseUpEvent(object sender, EventArgs e)
        {
            dragging = false;
        }

        private void SetTextureLocation(Texture t)
        {
            selectedTexture.Position = imagesPanel.PointToClient(Cursor.Position);
            imagesPanel.Controls[t.Name].Location = selectedTexture.Position;
            lstTextures.Refresh();   
        }

        private void SetTextureZOrder(Texture t, int newIndex)
        {
            t.ZIndex = newIndex;

            PictureBox pb = imagesPanel.Controls[t.Name] as PictureBox;


            imagesPanel.Controls.SetChildIndex(pb, newIndex);


            lstTextures.Items[lstTextures.SelectedIndex] = t;
            lstTextures.Refresh();
        }

        private void btnZOrderIncrease_Click(object sender, EventArgs e)
        {
            Texture t = lstTextures.Items[lstTextures.SelectedIndex] as Texture;
            SetTextureZOrder(t, t.ZIndex-1);

            splitContainer.SendToBack();
            eventPanel.BringToFront();
        }

        private void btnZOrderDecrease_Click(object sender, EventArgs e)
        {
            Texture t = lstTextures.Items[lstTextures.SelectedIndex] as Texture;
            SetTextureZOrder(t, t.ZIndex+1);

            splitContainer.SendToBack();
            eventPanel.BringToFront();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
